package com.xz.springbootvuebackend.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.springbootvuebackend.common.BaseResponse;
import com.xz.springbootvuebackend.common.ErrorCode;
import com.xz.springbootvuebackend.common.ResultUtils;
import com.xz.springbootvuebackend.domain.Furn;
import com.xz.springbootvuebackend.service.FurnService;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuzhou
 * @version 1.0
 * @createTime 2024-08-31
 * @description
 */
@RequestMapping("/furn")
@RestController
public class FunController {

    @Resource
    private FurnService furnService;

    @PostMapping("/save")
    public BaseResponse<Object> addFurn(@Validated @RequestBody Furn furn, Errors errors) {
        HashMap<String, Object> map = new HashMap<>();
        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(), fieldError.getDefaultMessage());
        }
        if (map.isEmpty()) {
            boolean save = furnService.save(furn);
            return ResultUtils.success(save);
        }
        return ResultUtils.error(ErrorCode.PARAMS_ERROR, map);
    }

    @GetMapping("/getByPage")
    public BaseResponse<IPage<Furn>> getList(@RequestParam(defaultValue = "5") long pageSize,
                                             @RequestParam(defaultValue = "1") long pageNum,
                                             @RequestParam(defaultValue = "", required = false) String search) {
        Page<Furn> furnPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Furn> furnQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.hasText(search)){
            furnQueryWrapper.like(Furn::getName, search);
        }
        return ResultUtils.success(furnService.page(furnPage, furnQueryWrapper));
    }

    @GetMapping("/getById")
    public BaseResponse<Furn> getById(@RequestParam("id") Integer id) {
        return ResultUtils.success(furnService.getById(id));
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody Furn furn) {
        return ResultUtils.success(furnService.updateById(furn));
    }

    @PostMapping("/delete/{id}")
    public BaseResponse<Boolean> delete(@PathVariable("id") Integer id) {
        return ResultUtils.success(furnService.removeById(id));
    }
}
